﻿using System;

using System.Data.SqlClient;

namespace IFD2
{
    public partial class FareTime : System.Web.UI.Page
    {
        //protected void Page_PreInit(object sender, EventArgs e)
        //{
        //    if (Request.QueryString["code"] != null)
        //    {
        //        lblFareCode.Text = Request.QueryString["code"];
        //        hidFareCode.Value = lblFareCode.Text;
        //    }
        //}

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("FA2") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }

                if (Request.QueryString["code"] != null)
                {
                    lblFareCode.Text = Request.QueryString["code"];
                    hidFareCode.Value = lblFareCode.Text;
                }
            }
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            GridView1.DataBind();
        }

        protected void butDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            
            string key;
            string[] strArray;
            if (Request.Form["KEY"] == null)
                return;
            key = Request.Form["KEY"];
            strArray = key.Split('|');

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            try
            {
                cmd.CommandText = "bll_fare_delete_timespan";
                SqlParameter para = cmd.Parameters.AddWithValue("@fare_code", lblFareCode.Text);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@time_from", strArray[0]);
                para = cmd.Parameters.AddWithValue("@time_to", strArray[1]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Fare not found or not proper status!";
                else if (ret_code == 2)
                    lblError.Text = "Failed!";
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void butAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("FA3") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            lblError.Text = "";
            int ret_code;
            try
            {
                SqlCommand cmd = new SqlCommand("bll_fare_add_timespan");
                SqlParameter para = cmd.Parameters.AddWithValue("@fare_code", lblFareCode.Text);
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@time_from", txtTimeFrom.Text);
                para = cmd.Parameters.AddWithValue("@time_to", txtTimeTo.Text);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done";
                    GridView1.DataBind();
                }
                else if (ret_code == 1)
                    lblError.Text = "Fare not found or not proper status!";
                else if (ret_code == 2)
                    lblError.Text = "Failed!";
                else if (ret_code == 3)
                    lblError.Text = "TimeFrom is later than TimeTo!";
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
    }
}